package com.hn.shopadmin.controller;

import com.github.pagehelper.PageInfo;
import com.hn.shopadmin.service.DictMasterService;
import com.hn.kiss.modules.sys.shopcommon.common.ParmException;
import com.hn.kiss.modules.sys.shopcommon.result.CommonPage;
import com.hn.kiss.modules.sys.shopdb.entity.DictMaster;
import com.hn.shopadmin.service.DictDetailService;
import com.hn.kiss.modules.sys.shopcommon.result.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

@Api(tags = "DictController", description = "字典主记录管理")
@RestController
@RequestMapping("/dictMaster")
@Validated
public class DictMasterController {
    @Autowired
    DictDetailService dictDetailService;
    @Autowired
    DictMasterService dictMasterService;

    @ApiOperation("字典主记录添加")
    @PostMapping("/add")
    public CommonResult addMaster(DictMaster dictMaster) throws ParmException {
        int count = dictMasterService.add(dictMaster);
        if (count > 0){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("字典主记录修改")
    @PutMapping("/update")
    public CommonResult updateMaster(DictMaster dictMaster) throws ParmException {
        int count = dictMasterService.update(dictMaster);
        if (count > 0){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("字典主记录分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "keywork", value = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/listPage")
    public CommonPage<DictMaster> listPageMaster(Integer page, Integer limit, String keywork){
        PageInfo<DictMaster> dictMasterPageInfo = dictMasterService.listPage(page, limit, keywork);
        return CommonPage.restPage(dictMasterPageInfo);
    }

    @ApiOperation("类别字典删除")
    @ApiImplicitParam(name = "typeCode", value = "字典类别代码", required = true, dataType = "String", paramType = "query")
    @DeleteMapping("/del")
    public  CommonResult delMaster(@NotBlank(message = "类别代码不能为空") String typeCode){
        int count = dictMasterService.del(typeCode);
        if (count > 0){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }




}
